package com.dd.ent.car.service.comment;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.dd.ent.car.common.bean.comment.Comment;
import com.dd.ent.car.common.bean.note.Note;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.common.config.UrlConfig;
import com.dd.ent.car.service.BaseService;
import com.pp.ent.base.log.MLog;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dengqu on 17/8/20.
 */

public class CommentService extends BaseService {

    public void sendComment(Comment comment) {
        notifyUI(UIConfig.SEND_COMMENT_SUCCESS, comment);
    }

    public void queryNoteCommentById(String noteId) {
        String url = UrlConfig.QUERY_NOTE_COMMENT_BY_NOTE_ID + "noteId=" + noteId;
        get(url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                notifyUI(UIConfig.QUERY_COMMENT_BY_NOTE_ID_SUCCESS, response == null ? null : response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MLog.error(TAG, error);
                notifyUI(UIConfig.QUERY_COMMENT_BY_NOTE_ID_FAIL);
            }
        });
    }

    public void queryVideoCommentById(String videoId) {
        String url = UrlConfig.QUERY_NOTE_COMMENT_BY_VIDEO_ID + "movieId=" + videoId;
        get(url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                notifyUI(UIConfig.QUERY_COMMENT_BY_VIDEO_ID_SUCCESS, response == null ? null : response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MLog.error(TAG, error);
                notifyUI(UIConfig.QUERY_COMMENT_BY_VIDEO_ID_SUCCESS);
            }
        });
    }


    public void addCommentToVideo(String movieId, String uid, String content, String createTime,String orginCommentId) {
        Map<String, String> params = new HashMap<>();
        params.put("movieId", movieId);
        params.put("uid", uid);
        params.put("content", content);
        params.put("createTime", createTime);
        params.put("originCommentId", orginCommentId);
        post(UrlConfig.ADD_COMMENT_TO_VIDEO, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                notifyUI(UIConfig.ADD_COMMENT_TO_VIDEO_SUCCESS);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MLog.error(TAG, error);
                notifyUI(UIConfig.ADD_COMMENT_TO_VIDEO_FAIL);
            }
        });
    }

    public void sumbitComment(Comment comment, Video video, String uid,String orginCommentId) {
        addCommentToVideo(video.getUuid(), uid, comment.getContent(), String.valueOf(comment.getCreateTime()),orginCommentId);
    }

    public void addCommentToNote(String noteId, String uid, String content, String createTime,String orginCommentId) {
        Map<String, String> params = new HashMap<>();
        params.put("noteId", noteId);
        params.put("uid", uid);
        params.put("content", content);
        params.put("createTime", createTime);
        params.put("originCommentId", orginCommentId);

        post(UrlConfig.ADD_COMMENT_TO_NOTE, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                notifyUI(UIConfig.ADD_COMMENT_TO_NOTE_SUCCESS);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MLog.error(TAG, error);
                notifyUI(UIConfig.ADD_COMMENT_TO_NOTE_FAIL);
            }
        });
    }

    public void sumbitComment(Comment comment,Note note, String uid,String orginCommentId) {
        addCommentToNote(note.getUuid(), uid, comment.getContent(), String.valueOf(comment.getCreateTime()),orginCommentId);
    }
}
